package com.psct.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.psct.R;
import com.psct.fragment.LoginFragment;
import com.psct.fragment.PersonCenterFragment;
import com.psct.fragment.RegistFragment;
import com.psct.social.SocialManager;
import com.psct.social.UserAccountManager;

public class PersonalActivity extends SherlockFragmentActivity {

	private RegistFragment registFragment;
	private PersonCenterFragment personFragment;
	private LoginFragment loginFragment;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		ActionBar actionBar = this.getSupportActionBar();
		actionBar.setBackgroundDrawable(getResources().getDrawable(R.color.actionbar_bg));
		actionBar.setDisplayHomeAsUpEnabled(true);
		if(UserAccountManager.getInstance().getUser(this)==null) {//没有登录过
			switchToLogin();
		}else {
			switchToPersonal();
		}

	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			this.finish();
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	/**
	 * 切换到登录界面
	 */
	public void switchToLogin() {
		if(loginFragment==null) {
			loginFragment=new LoginFragment();
		}
		FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
		ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
		ft.replace(android.R.id.content, loginFragment);
		ft.commit();
	}
	/**
	 * 切换到注册界面
	 */
	public void switchToRegisit() {
		if(registFragment==null) {
			registFragment=new RegistFragment();
		}
		FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
		ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
		ft.replace(android.R.id.content, registFragment);
		ft.addToBackStack(null);
		ft.commit();
	}
	
	/**
	 * 切换到个人资料
	 */
	public void switchToPersonal() {
		if(personFragment==null) {
			personFragment=new PersonCenterFragment();
		}
		FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
		ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
		ft.replace(android.R.id.content, personFragment);
		ft.commit();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		SocialManager.getInstance(getApplicationContext()).handlActivityResult(requestCode, resultCode, data);
	}
}
